import java.util.LinkedList;
import java.util.Queue;

public class Solution958 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        boolean ban=false;
        while (!queue.isEmpty()){
            TreeNode t=queue.poll();
            if (t.left==null && t.right!=null){
                return false;
            }
            if (t.left!=null){
                if (ban){
                    return false;
                }
                queue.offer(t.left);
            }
            if (t.right!=null){
                queue.offer(t.right);
            }
            if (t.right==null){
                ban=true;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new Solution958().isCompleteTree(new TreeNode(1,new TreeNode(2,new TreeNode(4),new TreeNode(5)),new TreeNode(3,new TreeNode(6),null))));
    }
}
